package cn.zhku.news.web.servlet.admin;

import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;
import cn.zhku.news.domain.News;
import cn.zhku.news.service.NewsService;
import cn.zhku.news.web.servlet.NewsServlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AdminNewsServlet
 */
@WebServlet("/AdminNewsServlet")
public class AdminNewsServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
    NewsService newsService  = new NewsService(); 
    
    public String query(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//
		response.setContentType("text/html;charset=utf-8"); 
		ArrayList<News> newsList = newsService.showAll();
		request.setAttribute("newsList", newsList);
		return "f:news/list.jsp";	
    }
	
    public String edit(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//
		response.setContentType("text/html;charset=utf-8");
		
		News news = CommonUtils.toBean(request.getParameterMap(), News.class);
		newsService.edit(news);
		request.setAttribute("msg", "恭喜您，新闻修改成功");
		this.query(request, response);
		return "f:news/list.jsp";
    }
    
    public String delete(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//
		response.setContentType("text/html;charset=utf-8");
		News news = CommonUtils.toBean(request.getParameterMap(), News.class);
		newsService.delete(news);
		request.setAttribute("msg", "恭喜您，新闻删除成功");
		this.query(request, response);
		return "f:news/list.jsp";
		}
}
